package com.lottery.ruok.lottrey.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.applozic.mobicomkit.ChatConfig;
import com.applozic.mobicomkit.contact.AppContactService;
import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.GetRedPacketHistoryAdapter;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetSendDetailResquest;
import com.lottery.ruok.lottrey.retrofitModule.request.GrabGiftHistoryResquest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetSendDetailResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.GrabGiftHistoryResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import glideHelp.GlideHelper;
import ui.ExtendListView;
import ui.NavigateBar;
import utils.Utils;

/*
* 发出红包
* */

public class GetPacketResultActivity extends BaseActivity {


    @BindView(R.id.money)
    TextView money;
    @BindView(R.id.avatar)
    CircleImageView avatar;
    @BindView(R.id.from_avatar)
    CircleImageView fromAvatar;
    @BindView(R.id.descp)
    TextView descp;
    @BindView(R.id.save)
    TextView save;
    @BindView(R.id.pin)
    ImageView pin;
    @BindView(R.id.now)
    TextView now;
    @BindView(R.id.total)
    TextView total;
    @BindView(R.id.list)
    ExtendListView list;
    @BindView(R.id.navigateBar)
    NavigateBar navigateBar;
    @BindView(R.id.got_all)
    TextView gotAll;
    @BindView(R.id.past_due)
    TextView pastDue;
    @BindView(R.id.check_wallet)
    TextView checkWallet;
    @BindView(R.id.not_all_layout)
    LinearLayout notAllLayout;
    private AppContactService appContactService;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_packet_result);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        appContactService = new AppContactService(this);
        if (getIntent().getBooleanExtra(ChatConfig.IS_SEND, false)) {
            RetrofitBase.getRetrofit().create(GetSendDetailResquest.class)
                    .getResult(UserInfo.getToken(), getIntent().getStringExtra(ChatConfig.PACKET_ID))
                    .compose(RxHelper.<GetSendDetailResponse>handleResult())
                    .subscribe(new RxSubscribe<GetSendDetailResponse>(this) {
                        @Override
                        protected void _onNext(GetSendDetailResponse getSendDetailResponse) {
                            ArrayList<GrabGiftHistoryResponse.ListBean> gets = new ArrayList<>();
                            for (GetSendDetailResponse.ListBean listBean : getSendDetailResponse.getList()) {
                                GrabGiftHistoryResponse.ListBean listBean1 = new GrabGiftHistoryResponse.ListBean();
                                listBean1.setAmount(listBean.getAmount());
                                listBean1.setAvatar(listBean.getAvatar());
                                listBean1.setId(listBean.getId());
                                listBean1.setUpdated_at(listBean.getUpdated_at());
                                listBean1.setUser_id(listBean.getUser_id());
                                listBean1.setNickname(listBean.getNickname());
                                gets.add(listBean1);
                            }
                            list.setAdapter(new GetRedPacketHistoryAdapter(GetPacketResultActivity.this, gets));

                            navigateBar.setCenterTitle("发出红包");
                            navigateBar.setOnOperateClickListener(new NavigateBar.OnOperateClickListener() {
                                @Override
                                public void onOperateClick(View view) {
                                    Intent intent = new Intent(GetPacketResultActivity.this, MyRedPacketActivity.class);
                                    startActivity(intent);
                                }
                            });
                            money.setText(getSendDetailResponse.getAmount() + "元");
                            if (Utils.isActivityRunning(GetPacketResultActivity.this)) {
                                GlideHelper.loadImage(GetPacketResultActivity.this, UserInfo.getAvatar(), avatar, R.mipmap.chat_default);
                            }
                            descp.setText(appContactService.getContactById(UserInfo.getUsername()).getDisplayName() + "发红包");
                            save.setVisibility(View.GONE);
                            switch (getSendDetailResponse.getGift_status()) {
                                case "0":
                                    gotAll.setVisibility(View.VISIBLE);
                                    break;
                                case "1":

                                    break;
                                case "2":
                                    notAllLayout.setVisibility(View.VISIBLE);
                                    pastDue.setText(String.format("没领完，剩余¥%s已返还到钱包", getSendDetailResponse.getReback_amount()));
                                    break;
                            }
                            now.setText("当前领取了" + (Integer.parseInt(getSendDetailResponse.getTotal()) - Integer.parseInt(getSendDetailResponse.getRemain())) + "/" + getSendDetailResponse.getTotal() + "个");
                            total.setText("共" + getSendDetailResponse.getAmount() + "元");
                        }

                        @Override
                        protected boolean showDialog() {
                            return false;
                        }
                    });
        } else {
            RetrofitBase.getRetrofit().create(GrabGiftHistoryResquest.class)
                    .getResult(UserInfo.getToken(), getIntent().getStringExtra(ChatConfig.PACKET_ID), getIntent().getStringExtra(ChatConfig.PACKET_GET_ID))
                    .compose(RxHelper.<GrabGiftHistoryResponse>handleResult())
                    .subscribe(new RxSubscribe<GrabGiftHistoryResponse>(this) {
                        @Override
                        protected void _onNext(GrabGiftHistoryResponse grabGiftHistoryResponse) {
                            if (UserInfo.getNickname().equals(grabGiftHistoryResponse.getTo_nickname())) {
                                save.setVisibility(View.VISIBLE);
                                save.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        Intent intent = new Intent(GetPacketResultActivity.this, AccountDetailActivity.class);
                                        startActivity(intent);
                                    }
                                });
                                navigateBar.setOnOperateClickListener(new NavigateBar.OnOperateClickListener() {
                                    @Override
                                    public void onOperateClick(View view) {
                                        Intent intent = new Intent(GetPacketResultActivity.this, MyRedPacketActivity.class);
                                        startActivity(intent);
                                    }
                                });
                            } else {
                                navigateBar.setOnOperateClickListener(new NavigateBar.OnOperateClickListener() {
                                    @Override
                                    public void onOperateClick(View view) {
                                        Intent intent = new Intent(GetPacketResultActivity.this, MyRedPacketActivity.class);
                                        startActivity(intent);
                                    }
                                });
                                save.setVisibility(View.GONE);
                            }
                            if (Utils.isActivityRunning(GetPacketResultActivity.this)) {
                                GlideHelper.loadImage(GetPacketResultActivity.this, grabGiftHistoryResponse.getFrom_avatar(), avatar);
                                GlideHelper.loadImage(GetPacketResultActivity.this, grabGiftHistoryResponse.getTo_avatar(), fromAvatar);
                            }
                            money.setText(grabGiftHistoryResponse.getAmount() + "元");
                            descp.setText(appContactService.getContactById(grabGiftHistoryResponse.getTo_nickname()).getDisplayName() + "领取了" + appContactService.getContactById(grabGiftHistoryResponse.getFrom_nickname()).getDisplayName() + "的红包");
                            if (grabGiftHistoryResponse.getType().equals("3")) {
                                pin.setVisibility(View.VISIBLE);
                            } else {
                                pin.setVisibility(View.GONE);
                            }
                            now.setText("当前领取了" + (Integer.parseInt(grabGiftHistoryResponse.getTotal()) - Integer.parseInt(grabGiftHistoryResponse.getRemain())) + "/" + grabGiftHistoryResponse.getTotal() + "个");
                            total.setText("共" + grabGiftHistoryResponse.getTotal_amount() + "元");

                            list.setAdapter(new GetRedPacketHistoryAdapter(GetPacketResultActivity.this, grabGiftHistoryResponse.getList()));
                        }

                        @Override
                        protected boolean showDialog() {
                            return false;
                        }
                    });
        }
    }

    @OnClick(R.id.check_wallet)
    public void onViewClicked() {
        Intent intent_red = new Intent(this, AccountDetailActivity.class);
        startActivity(intent_red);
    }
}